import Dayjs from 'dayjs'
import zh from 'dayjs/locale/zh-cn'
import LocalizedFormat from 'dayjs/plugin/localizedFormat'
import relativeTime from 'dayjs/plugin/relativeTime'
const customizedLocale = {
    ...zh,
    meridiem: (hour, minute, isLowercase) => (hour > 12 ? '下午' : '上午')
}
Dayjs.locale(customizedLocale)
Dayjs.extend(LocalizedFormat)
Dayjs.extend(relativeTime)

export const dayjs = Dayjs

export function format(value, config = 'YYYY年M月D日') {
    let currentValue = value
    if (!dayjs.isDayjs(value)) {
        if (dayjs(value).isValid() && value) {
            currentValue = dayjs(value)
        } else {
            currentValue = {
                format() {
                    return ''
                }
            }
        }
    }
    return currentValue.format(config)
}
export function formatTime(time, option) {
    // time = +time * 1000
    const d = new Date(time)
    const now = Date.now()
    const diff = (now - d) / 1000
    if (diff < 30) {
        return '刚刚'
    } else if (diff < 3600) {
        // less 1 hour
        return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
        return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
        return '1天前'
    }
    if (option) {
        return format(time, option)
    }
    return format(time, 'YYYY-MM-DD')
}
